﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace EscPrinterVfpV1d0.Service
{
    //愛普生TM-220打印機Esc打印函數
    public class PrintFunctionV1d0
    {
        ///// <summary>
        ///// 打印機IP地址
        ///// </summary>
        //public string PrinterNetIp { get; set; }
        ///// <summary>
        ///// 打印機端口號
        ///// </summary>
        //public int PrinterNetPort { get; set; }
        /// <summary>
        /// 打印機Tcp客戶端連接套接字
        /// </summary>
        public TcpClient PrinterTcpClient { get; set; }
        /// <summary>
        /// 打印機連接超時時間
        /// </summary>
        public int ConnectTimeoutSecond { get; set; }
        /// <summary>
        /// 打印機常駐漢字字符類別
        /// </summary>
        public string PrinterResidentChineseCharacter { get; set; }
        /// <summary>
        /// 紙寬
        /// </summary>
        public int PaperWidth { get; set; }
        /// <summary>
        /// 字符間隔
        /// </summary>
        public int DotSpaceBetweenCharacter { get; set; }
        /// <summary>
        /// 每行的字符個數
        /// </summary>
        public int CharactersPerLine { get; set; }


        public PrintFunctionV1d0(TcpClient tcpClient)
        {
            //PrinterNetIp = "192.168.3.99";
            //PrinterNetPort = 9100;
            PrinterTcpClient = null;
            PrinterResidentChineseCharacter = "Triditional";
            PaperWidth = 76;
            DotSpaceBetweenCharacter = 3;
            CharactersPerLine = 40;
            ConnectTimeoutSecond = 3;
            PrinterTcpClient = tcpClient;
        }

        private void DisConnnect()
        {
            if (PrinterTcpClient != null)
                PrinterTcpClient.Close();
        }
        /// <summary>
        /// 发送自己指令到打印机
        /// </summary>
        /// <param name="command">字节指令</param>
        /// <returns></returns>
        public bool SendByteCommand(byte[] command)
        {
            bool isSuccess = false;
            try
            {
                if (PrinterTcpClient!=null&&PrinterTcpClient.Connected)
                {
                    NetworkStream netStream = PrinterTcpClient.GetStream();
                    netStream.Write(command, 0, command.Length);
                    netStream.Flush();

                    isSuccess = true;
                }                
            }
            catch (Exception ex)
            {
                //SimpleLoger.Instance.Error(ex);
                isSuccess = false;
            }
            return isSuccess;
        }


        /// <summary>
        /// 发送字符串打打印机
        /// </summary>
        /// <param name="content">待打印字符串</param>
        /// <param name="encodingName">待打印机字符串的编码名称</param>
        /// <returns></returns>
        public bool SendStringToPrinter(string content,string encodingName="Big5")
        {
            bool isSuccess = false;
            try
            {
                Encoding currEncoding=Encoding.GetEncoding(encodingName);
                byte[] printByteArray = currEncoding.GetBytes(content);
                isSuccess=SendTextByteArrayToPrinter(printByteArray);
            }
            catch (Exception ex)
            {
               // SimpleLoger.Instance.Error(ex);
            }
            return isSuccess;
        }

        /// <summary>
        /// 将文本的已编码字节数组发送至打印机
        /// </summary>
        /// <param name="textBytes">文本字节数据</param>
        /// <returns></returns>
        private bool SendTextByteArrayToPrinter(byte[] textBytes)
        {
            bool isSuccess = false;
            try
            {
                if (PrinterTcpClient.Connected)
                {
                    NetworkStream netStream = PrinterTcpClient.GetStream();
                    netStream.Write(textBytes, 0, textBytes.Length);
                    netStream.Flush();
                    isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                //SimpleLoger.Instance.Error(ex);
            }
            return isSuccess;
        }


        public bool PrintAscTextAsTextMode(string contentText)
        {
            bool result = false;
            //默认打印模式
            byte AscPrintMode = 0;
            SendByteCommand(TMU220PrinterCmdUtilsV1d0.AscFontPrintMode(AscPrintMode));
            SendStringToPrinter(contentText);
            return result;
        }


    }
}
